package com.fzk.blog.mapper;

import com.fzk.blog.bean.vo.BlogInfo;
import org.apache.ibatis.builder.annotation.ProviderMethodResolver;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * @author fzk
 * @date 2021-08-22 21:08
 * <p>
 * 提供动态BlogMapper的SQL语句
 */
public class BlogSqlProvider implements ProviderMethodResolver {
    public static String getBlogInfoPage(final String title, final Integer typeId, final Integer userId, final Integer[] tagId, final Boolean published, final Integer flag, final int index, final int pageSize) {
        StringBuilder str = new StringBuilder();
        if (tagId != null && tagId.length != 0) {
            str.append("(");
            int len = tagId.length;
            for (int i = 0; i < (len - 1); ++i) {
                str.append(tagId[i]);
                str.append(",");
            }
            str.append(tagId[len - 1]);
            str.append(")");
        }
        return new SQL() {{
            SELECT("b.id,b.type_id typeId,b.recommend_level recommendLevel,b.create_flag createFlag,b.title,b.description,b.image_url imageUrl,b.view_times viewTimes,b.published,b.publish_date publishDate,t.name typeName,b.create_by createBy,b.edit_time editTime");
            FROM("tbl_blog b");
            LEFT_OUTER_JOIN("tbl_type t ON (b.type_id=t.id AND t.flag=0)");
            if (tagId != null && tagId.length != 0) {
                INNER_JOIN("tbl_blog_tag_reference r ON(b.id=r.blog_id AND r.flag=0)");
                INNER_JOIN("tbl_tag tag ON(r.tag_id=tag.id AND tag.flag=0)");
            }
            if (flag != null) {
                WHERE("b.flag=#{flag}");
            } else {
                WHERE("b.flag=0");
            }
            if (title != null) {
                WHERE("b.title LIKE '%' #{title} '%' ");
            }
            if (typeId != null) {
                WHERE("type_id=#{typeId}");
            }
            if (userId != null) {
                WHERE("b.create_by=#{userId}");
            }
            if (published != null) {
                WHERE("b.published=#{published} ");
            }
            if (tagId != null && tagId.length != 0) {
                WHERE("tag.id IN " + str.toString());
            }
            GROUP_BY("b.id");
            ORDER_BY("b.recommend_level DESC,b.id ASC");
            LIMIT("#{pageSize}");
            OFFSET("#{index}");
        }}.toString();
    }

    public static String getBlogIdList(String title, Integer typeId, Integer userId, Integer[] tagId, Boolean published, Integer flag) {
        StringBuilder str = new StringBuilder();
        if (tagId != null && tagId.length != 0) {
            str.append("(");
            int len = tagId.length;
            for (int i = 0; i < (len - 1); ++i) {
                str.append(tagId[i]);
                str.append(",");
            }
            str.append(tagId[len - 1]);
            str.append(")");
        }
        return new SQL() {{
            SELECT("b.id");
            FROM("tbl_blog b");
            LEFT_OUTER_JOIN("tbl_type t ON (b.type_id=t.id AND t.flag=0)");
            if (tagId != null && tagId.length != 0) {
                INNER_JOIN("tbl_blog_tag_reference r ON(b.id=r.blog_id AND r.flag=0)");
                INNER_JOIN("tbl_tag tag ON(r.tag_id=tag.id AND tag.flag=0)");
            }
            if (flag != null) {
                WHERE("b.flag=#{flag}");
            } else {
                WHERE("b.flag=0");
            }
            if (title != null) {
                WHERE("b.title LIKE '%' #{title} '%' ");
            }
            if (typeId != null) {
                WHERE("b.type_id=#{typeId}");
            }
            if (userId != null) {
                WHERE("b.create_by=#{userId}");
            }
            if (published != null) {
                WHERE("b.published=#{published} ");
            }
            if (tagId != null && tagId.length != 0) {
                WHERE("tag.id IN " + str.toString());
            }
            GROUP_BY("b.id");
            ORDER_BY("b.recommend_level DESC,b.id ASC");
        }}.toString();
    }

    public static String getBlogInfoList(String title, String username, Integer typeId, Boolean published, Integer flag) {
        return new SQL() {{
            SELECT("b.id,b.type_id typeId,b.recommend_level recommendLevel,b.create_flag createFlag,b.title,b.description,b.image_url imageUrl,b.view_times viewTimes,b.published,b.publish_date publishDate,t.name typeName,b.create_by createBy,b.edit_time editTime");
            FROM("tbl_blog b");
            LEFT_OUTER_JOIN("tbl_type t ON (b.type_id=t.id AND t.flag=0) ");
            LEFT_OUTER_JOIN("tbl_user u ON (b.create_by=u.id AND u.flag=0) ");
            if (flag != null) {
                WHERE("b.flag=#{flag}");
            }
            if (title != null) {
                WHERE("b.title LIKE '%' #{title} '%'  ");
            }
            if (username != null) {
                WHERE("u.username LIKE '%' #{username} '%' ");
            }
            if (typeId != null) {
                WHERE("b.type_id=#{typeId} ");
            }
            if (published != null) {
                WHERE("b.published=#{published} ");
            }
            ORDER_BY("b.type_id ASC,b.recommend_level DESC ");

        }}.toString();
    }
}
